package com.aiti.lulian.controller;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardGovNewsDto;
import com.aiti.lulian.entity.CardGovNewsEntity;
import com.aiti.lulian.service.CardGovNewsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 政府名片最新资讯表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-03-04
 */
@RestController
@RequestMapping("/govNews")
public class CardGovNewsController extends WebController {

    @Autowired
    private CardGovNewsService cardGovNewsService;

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public Message list(CardGovNewsDto dto) {
        String govId = dto.getGovId();
        Page page = startPage();
        Page resultPage = cardGovNewsService.page(page, new LambdaQueryWrapper<CardGovNewsEntity>().eq(CardGovNewsEntity::getGovId, govId).orderByDesc(CardGovNewsEntity::getBaseCreateTime));
        return Success(getPageResult(resultPage.getRecords(), dto.getPageNum(), dto.getPageSize(), (int) resultPage.getTotal()));
    }


    @ApiOperation(value = "新增")
    @PostMapping("/insert")
    public Message insert(CardGovNewsDto dto) {
        String govId = dto.getGovId();
        CardGovNewsEntity cardGovNewsEntity = new CardGovNewsEntity();
        BeanUtils.copyProperties(dto, cardGovNewsEntity);
        cardGovNewsService.save(cardGovNewsEntity);
        return Success();
    }

    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public Message update(CardGovNewsDto dto) {
        String baseId = dto.getBaseId();
        if(!StringUtils.isEmpty(baseId)){
            CardGovNewsEntity cardGovNewsEntity = new CardGovNewsEntity();
            BeanUtils.copyProperties(dto, cardGovNewsEntity);
            cardGovNewsService.updateById(cardGovNewsEntity);
        }
        return Success();
    }


    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public Message delete(CardGovNewsDto dto) {
        String baseId = dto.getBaseId();
        if(!StringUtils.isEmpty(baseId)){
            cardGovNewsService.removeById(baseId);
        }
        return Success();
    }


}
